package servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;

import bean.Content;
import dao.UserDao;
import service.ApiService;
import utils.FileUtils;

public class ApiServlet extends HttpServlet {
	private static final Logger logger = LoggerFactory.getLogger(ApiServlet.class);
	private static final long serialVersionUID = 1L;
	

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.setHeader("content-type","text/html;charset=UTF-8");
		doGet(request, response);
	}
	
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.setHeader("content-type","text/html;charset=UTF-8");
		String method = (String)request.getParameter("method");
		if(method==null) {
			PrintWriter out = response.getWriter();
			out.println("invalid request!");
		} else if(method.equals("query")) {
			Query(request, response);
		}
	}

	/** 
	* @Title: Query 
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @param @param request
	* @param @param response
	* @param @throws ServletException
	* @param @throws IOException    设定参数 
	* @return void    返回类型 
	* @throws 
	* @author undoner@gmail.com 
	* @date 2017年1月23日 下午12:57:37 
	*/
	protected void Query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String filePath = (request.getParameter("filePath") == null||request.getParameter("filePath").equals("")) ? "None" : request.getParameter("filePath");
		String SearchKey = (request.getParameter("SearchKey") == null||request.getParameter("SearchKey").equals("")) ? "None" : request.getParameter("SearchKey");
		int upLineConunt = (request.getParameter("upLineConunt") == null||request.getParameter("upLineConunt").equals("")) ? 0: Integer.parseInt((String) request.getParameter("upLineConunt"));
		int downLineCount = (request.getParameter("downLineCount") == null||request.getParameter("downLineCount").equals("")) ? 0: Integer.parseInt((String) request.getParameter("downLineCount"));
		int limit = (request.getParameter("limit") == null||request.getParameter("limit").equals("")) ? 1: Integer.parseInt((String) request.getParameter("limit"));
		
		logger.debug("filePath:"+filePath+",SearchKey:"+SearchKey+",upLineConunt:"+upLineConunt+",downLineCount:"+downLineCount+",limit:"+limit);
		
		// 检查参数是否为空
		PrintWriter out = response.getWriter();
		if (filePath .equals("None") || SearchKey.equals("None")) {
			out.println("Query Parameter is Null: filePath || SearchKey");
			return;
		}
		
		//调用service处理查询逻辑
		ApiService apiService =new ApiService() ;
		String rst = apiService.Query(filePath, SearchKey, upLineConunt, downLineCount, limit);
				 
		//返回json数据给客户端
		out.println(rst);
	}

}
